home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / doslynx / src / capstdio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  6.3 KB  |  266 lines

  1. /*
  2.  *    Copyright (c) 1994, University of Kansas, All Rights Reserved
  3.  *
  4.  *    Include File:    capstdio.h
  5.  *    Purpose:    Provide functions to capture output to stdio and
  6.  *            stderr.
  7.  *    Remarks/Portability/Dependencies/Restrictions:
  8.  *        We do not include the capstdio header file because it would
  9.  *        further screw up any of the stdio function names that we may
  10.  *        call in the below code.
  11.  *    Revision History:
  12.  *        01-08-94    created
  13.  */
  14. #include"capalloc.h"
  15. #include<stdio.h>
  16. #include<stdarg.h>
  17. #include<string.h>
  18. #include<errno.h>
  19.  
  20. int capfprintf(FILE *Fp_stream, const char *cp_format, ...)    {
  21. /*
  22.  *    Purpose:    Check if output is directed at stdout or stderr and
  23.  *            if so redirect to a user implemented function.
  24.  *    Arguments:    Fp_stream    The destination file.
  25.  *            cp_format    The format string describing the stack
  26.  *            ...        Variable number of arguments matching
  27.  *                    the stack.
  28.  *    Return Value:    int    On error, EOF.
  29.  *                On success, the number of bytes written.
  30.  *    Remarks/Portability/Dependencies/Restrictions:
  31.  *        We must limit the amount to be written to stdout and stderr
  32.  *        by a set buffersize of 1024 bytes.
  33.  *    Revision History:
  34.  *        01-08-94    created
  35.  */
  36.  
  37.     va_list argptr;
  38.     int retval;
  39.  
  40.     /*
  41.      *    Set up our variable number of arguments.
  42.      */
  43.     va_start(argptr, cp_format);
  44.  
  45.     /*
  46.      *    Do we capture the output?
  47.      */
  48.     if(Fp_stream == stdout || Fp_stream == stderr)    {
  49.         /*
  50.          *     Capture.
  51.          */
  52.         char ca_buffer[1024];
  53.         retval = vsprintf(ca_buffer, cp_format, argptr);
  54.         if(Fp_stream == stdout)
  55.             cap_stdout(ca_buffer);
  56.         else
  57.             cap_stderr(ca_buffer);
  58.     }
  59.     else    {
  60.         /*
  61.          *    Normal.
  62.          */
  63.         retval = vfprintf(Fp_stream, cp_format, argptr);
  64.     }
  65.  
  66.     va_end(argptr);
  67.     return(retval);
  68. }
  69.  
  70. int capfputs(const char *cp_s, FILE *Fp_stream)    {
  71. /*
  72.  *    Purpose:    Check if output is directed at stdout or stderr and
  73.  *            if so redirect.
  74.  *    Arguments:    cp_s        The string to output.
  75.  *            Fp_stream    The file to write the output to.
  76.  *    Return Value:    int    On error, EOF.
  77.  *                On success, the last character written.
  78.  *    Remarks/Portability/Dependencies/Restrictions:
  79.  *    Revision History:
  80.  *        01-08-94    created
  81.  */
  82.     if(EOF == capfprintf(Fp_stream, "%s", cp_s))
  83.         return(EOF);
  84.     else
  85.         return(*(cp_s + strlen(cp_s) - 1));
  86. }
  87.  
  88. size_t capfwrite(const void *vp, size_t st_size, size_t st_n,
  89.     FILE *Fp_stream)    {
  90. /*
  91.  *    Purpose:    Check if output is directed at stdout or stderr and
  92.  *            if so redirect.
  93.  *    Arguments:    vp        A pointer to objects of any size.
  94.  *            st_size        The size of the objects to write.
  95.  *            st_n        The number of objects to write.
  96.  *            Fp_stream    The output stream.
  97.  *    Return Value:    size_t    The number of items written.
  98.  *                On success, returns st_n.
  99.  *                On failure, returns < st_n.
  100.  *    Remarks/Portability/Dependencies/Restrictions:
  101.  *    Revision History:
  102.  *        01-08-94    created
  103.  */
  104.  
  105.     size_t retval;
  106.     char *cp_traverse = vp;
  107.  
  108.     /*
  109.      *    Do we capture the output?
  110.      */
  111.     if(Fp_stream == stdout || Fp_stream == stderr)    {
  112.         /*
  113.          *    capture
  114.          */
  115.         size_t st_items;
  116.         size_t st_bytes;
  117.  
  118.         /*
  119.          *    Loop through the items.
  120.          */
  121.         for(st_items = (size_t)0; st_items < st_n; st_items++)    {
  122.             /*
  123.              *    Loop through the bytes.
  124.              */
  125.             for(st_bytes = (size_t)0; st_bytes < st_size;
  126.                 st_bytes++)    {
  127.                 /*
  128.                  *    print one byte at a time.
  129.                  */
  130.                 capfprintf(Fp_stream, "%c", *cp_traverse++);
  131.             }
  132.         }
  133.  
  134.         retval = st_n;
  135.     }
  136.     else    {
  137.         /*
  138.          *    normal
  139.          */
  140.         retval = fwrite(vp, st_size, st_n, Fp_stream);
  141.     }
  142.  
  143.     return(retval);
  144. }
  145.  
  146. void capperror(const char *cp_s)    {
  147. /*
  148.  *    Purpose:    Redirect stderr output.
  149.  *    Arguments:    cp_s    A message.
  150.  *    Return Value:    void
  151.  *    Remarks/Portability/Dependencies/Restrictions:
  152.  *    Revision History:
  153.  *        01-08-94    created
  154.  */
  155.  
  156.     capfprintf(stderr, "%s : %s", cp_s, strerror(errno));
  157. }
  158.  
  159. int capprintf(const char *cp_format, ...)    {
  160. /*
  161.  *    Purpose:    Redirect stdout output.
  162.  *    Arguments:    cp_format    The printf style format string.
  163.  *            ...        Variable arguments corresponding to
  164.  *                    the format string.
  165.  *    Return Value:    int    On Error, returns EOF.
  166.  *                On success, returns number of bytes written.
  167.  *    Remarks/Portability/Dependencies/Restrictions:
  168.  *        The amount written is limited to 1024 bytes.
  169.  *    Revision History:
  170.  *        01-08-94    created
  171.  */
  172.  
  173.     int retval;
  174.     char ca_buffer[1024];
  175.     va_list argptr;
  176.  
  177.     /*
  178.      *    Handle variable number of arguments.
  179.      */
  180.     va_start(argptr, cp_format);
  181.  
  182.     /*
  183.      *    redirect stdout
  184.      */
  185.     retval = vsprintf(ca_buffer, cp_format, argptr);
  186.     cap_stdout(ca_buffer);
  187.  
  188.     va_end(argptr);
  189.     return(retval);
  190. }
  191.  
  192. int capputc(int c, FILE *Fp_stream)    {
  193. /*
  194.  *    Purpose:    Captures output directed at stdio and stderr and
  195.  *            redirects.
  196.  *    Arguments:    c        The character to output.
  197.  *            Fp_stream    The output stream.
  198.  *    Return Value:    int    On success, c.
  199.  *                On failure, EOF.
  200.  *    Remarks/Portability/Dependencies/Restrictions:
  201.  *    Revision History:
  202.  *        01-08-94    created
  203.  */
  204.  
  205.     return(capfprintf(Fp_stream, "%c", c) == EOF ? EOF : c);
  206. }
  207.  
  208. #ifdef WATTCP
  209. /*
  210.  *    The following are to take over some wattcp assembly functions that
  211.  *    write directly to the screen.
  212.  *    Assume wattcp would only do this in the case of an error, so put
  213.  *    to stderr, the above functions will handle the rest.
  214.  */
  215.  
  216. void capoutch(char c)    {
  217. /*
  218.  *    Purpose:    Capture output meant for screen and redirect.
  219.  *    Arguments:    c    The character to output.
  220.  *    Return Value:    void
  221.  *    Remarks/Portability/Dependencies/Restrictions:
  222.  *    Revision History:
  223.  *        01-08-94    created
  224.  */
  225.  
  226.     capputc(c, stderr);
  227. }
  228.  
  229. void capouts(char *cp)    {
  230. /*
  231.  *    Purpose:    Capture output meant for screen and redirect.
  232.  *    Arguments:    cp    The string to output.
  233.  *    Return Value:    void
  234.  *    Remarks/Portability/Dependencies/Restrictions:
  235.  *    Revision History:
  236.  *        01-08-94    created
  237.  */
  238.     capfputs(cp, stderr);
  239. }
  240.  
  241. void capouthex(char c)    {
  242. /*
  243.  *    Purpose:    Capture output meant for screen and redirect.
  244.  *    Arguments:    c    The character to output in hex.
  245.  *    Return Value:    void
  246.  *    Remarks/Portability/Dependencies/Restrictions:
  247.  *    Revision History:
  248.  *        01-08-94    created
  249.  */
  250.  
  251.     char c_temp;
  252.  
  253.     /*
  254.      *    Print the first hex digit.
  255.      */
  256.     c_temp = c >> 4;
  257.     c_temp = (c_temp < 10) ? '0' + c_temp : 'A' + c_temp - 10;
  258.  
  259.     /*
  260.      *    Printf the second hex digit.
  261.      */
  262.     c_temp = c & 0x0F;
  263.     c_temp = (c_temp < 10) ? '0' + c_temp : 'A' + c_temp - 10;
  264.     capfprintf(stderr, "%c", c_temp);
  265. }
  266. #endif /* WATTCP */